package game;

import game.manager.PersonManager;
import game.modifier.PersonModifier;
import game.screen.ProfileScreen;
import game.util.InterestUtil.Interest;
import game.util.ProfileUtil;
import game.util.TraitUtil.Trait;
import game.util.Util;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Vector;

public class Person {

	public enum Gender {
		MALE("Male"), FEMALE("Female");

		private String text;

		Gender(String text) {
			this.text = text;
		}

		public static Gender fromString(String text) {
			if (text != null) {
				for (Gender gender : Gender.values()) {
					String genderName = gender.text;
					if (text.equalsIgnoreCase(genderName)
							|| genderName.toLowerCase().startsWith(
									text.toLowerCase())) {
						return gender;
					}
				}
			}
			return null;
		}
	}

	public enum Qualities {
		FUN, OUTGOING, INDEPENDANT, STRONG, TOUGH, SWEET
	}

	public enum Height {
		TINY, AVERAGE, TALL
	}

	public enum Haircolor {
		BROWN, BLONDE
	}

	public enum Orientation {
		STRAIGHT, GAY, BISEXUAL
	}

	public enum Professions {
		TEACHER, DESIGNER, NURSE, ELECTRICIAN, CARPENTER, PROGRAMMER, ACTOR, MUSICIAN, ENGINEER, JANITOR, NANNY
	}

	private Collection<Interest> interests;
	private Collection<PersonModifier> modifiers;
	private Collection<Trait> traits;
	private Gender gender;
	private Orientation orientation;
	private String firstName;
	private String lastName;
	private int looks;
	private int cash;
	private int energy;
	private ProfileScreen profile;

	public Person(String firstName, String lastName, Collection<Trait> traits,
			Collection<Interest> interests, Gender gender, int looks,
			Orientation orientation) {
		this.firstName = new String(firstName);
		this.lastName = new String(lastName);
		this.traits = traits;
		this.interests = new Vector<Interest>(interests);
		this.modifiers = new Vector<PersonModifier>();
		this.gender = gender;
		this.looks = looks;
		this.orientation = orientation;
		this.cash = 0;
		this.energy = 0;
	}

	public Person() {
		this.firstName = new String();
		this.lastName = new String();
		this.traits = new Vector<Trait>();
		this.interests = new Vector<Interest>();
		this.modifiers = new Vector<PersonModifier>();
		this.orientation = Orientation.BISEXUAL;
		this.cash = 0;
		this.energy = 0;
	}

	public ProfileScreen getProfile() {
		if (this.profile == null) {
			ProfileUtil generator = new ProfileUtil();
			this.profile = generator.generateProfile(this);
		}
		return profile;
	}

	@Override
	public String toString() {
		return "Person [" + getFullName() + ", " + gender + ", " + traits
				+ ", " + interests + ", " + looks + ", " + orientation + "]";
	}

	public String getFullName() {
		return this.firstName + " " + this.lastName;
	}

	public Orientation getOrientation() {
		return this.orientation;
	}

	public List<Trait> getTraits() {
		return new Vector<Trait>(this.traits);
	}

	public String getFirstName() {
		return new String(this.firstName);
	}

	public String getLastName() {
		return new String(this.lastName);
	}

	public Gender getGender() {
		return this.gender;
	}

	public int getBaseLooks() {
		return this.looks;
	}

	public int getModifiedLooks() {
		int looks = getBaseLooks();
		for (PersonModifier modifier : modifiers) {
			looks += modifier.getLooksModify();
		}
		return looks;
	}

	public Collection<Interest> getInterests() {
		return new Vector<Interest>(this.interests);
	}

	public void setFirstName(String firstName) {
		this.firstName = new String(firstName);
		Util.log("First name set to " + firstName + "\n");
	}

	public void setLastName(String lastName) {
		this.lastName = new String(lastName);
		Util.log("Last name set to " + lastName + "\n");
	}

	public void setGender(Gender gender) {
		this.gender = gender;
		Util.log("Gender set to " + gender + "\n");
	}

	public int getCash() {
		return this.cash;
	}

	public int getEnergy() {
		return this.energy;
	}

	public void addModifier(PersonModifier modifier) {
		this.modifiers.add(modifier);
	}

	public void setTraits(Collection<Trait> traits) {
		this.traits = new Vector<Trait>(traits);
		Util.log("Traits set to " + traits + "\n");
	}

	public void setInterests(Collection<Interest> interests) {
		this.interests = new Vector<Interest>(interests);
		Util.log("Interests set to " + interests + "\n");
	}

	public void expireModifiers(Date date) {
		final Vector<PersonModifier> expiredModifiers = new Vector<PersonModifier>(
				this.modifiers.size());

		for (PersonModifier modifier : this.modifiers) {
			if (modifier.isExpired(date))
				expiredModifiers.add(modifier);
		}

		for (PersonModifier expiredModifier : expiredModifiers) {
			this.modifiers.remove(expiredModifier);
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Person) {
			Person other = (Person) obj;
			return other.cash == this.cash && other.energy == this.energy
					&& other.firstName.equals(this.firstName)
					&& other.lastName.equals(this.lastName)
					&& other.looks == this.looks
					&& other.orientation == this.orientation;
		}
		return false;
	}

	public void processMessage(Message message) {
		message.getCommunication().getReplies();
	}

	public boolean isUser() {
		return this == PersonManager.getManager().getUser();
	}

	public void setEnergy(int energy) {
		this.energy = energy;
		Util.log("Energy set to " + this.energy + "\n");
	}

	public void setCash(int cash) {
		this.cash = cash;
		Util.log("Cash set to " + this.cash + "\n");
	}
}
